
mui.init({
    beforeback:function(){
        var obj = plus.webview.getWebviewById("src/templates/columns/questionBank.html");
        console.log(obj);
        //触发列表界面的自定义事件（dataList）,从而进行数据刷新
        mui.fire(obj, 'refreshData');
    },
});

var answerResultsData;
mui.plusReady(function () {
	//禁用ios右滑返回
    plus.webview.currentWebview().setStyle({ 'popGesture': 'none' });
    var dati_jilu = JSON.parse(plus.storage.getItem('dati_jilu'));
    var api_host = plus.storage.getItem('HOST_URL');
    var self = plus.webview.currentWebview();
    var category = self.category;
    var category_id = self.category_id;
    answerResultsData = new Vue({
        el: '#QUEdata',
        data: {
            data: [],
        },
        mounted: function () {
            var self = this;
            showWatting();
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: api_host + "/api/questionbank/choice_question/check_user_answer",
                data: {
                    'Answer': dati_jilu,
                    'category': category,
                    'category_id': category_id
                },
                headers:{'User-Token':plus.storage.getItem('UserToken')},
                timeout:3000,
                success: function (msg) {
                    self.data = msg;
                    self.$nextTick(function () {
                        wave();
                    })
                },
                error: function () {
                    mui.toast('加载失败!', {duration: '200',type: 'div'});
                },
                complete:function () {
                    plus.nativeUI.closeWaiting();
                }
            });
        },
        methods: {
            look_false: function () {
                //打开错题解析页面
                mui.openWindow({
                    url: 'mistakeRevolution.html',
                    extras: {
                        'type': 'ct',
                        'data': this.data.data,
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            look_all: function () {
                //打开错题解析页面
                mui.openWindow({
                    url: 'mistakeRevolution.html',
                    extras: {
                        'type': 'all',
                        'data': this.data.data,
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            reset_questions: function () {
                //在来一题
                mui.openWindow({
                    url: '../columns/questionBank.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            }
        }
    })
})

// 动态改变webview的侧滑返回功能：
// var wv = plus.webview.currentWebview();

/*plus.webview.currentWebview().addEventListener( "popGesture", function(e){
    plus.webview.close(wv);
    mui.openWindow({
        url: '../columns/questionBank.html',
        createNew: false,
        show: {
            autoShow: true,
            aniShow: 'pop-in',
            duration: 400
        },
        waiting: {
            autoShow: false,
        }
    });
});*/

mui.back = function () {
    mui.toast('准备返回...', {duration: '900',type: 'div'});
    mui.later(function () {
        mui.openWindow({
            url: '../columns/questionBank.html',
            createNew: false,
            show: {
                autoShow: true,
                aniShow: 'pop-in',
                duration: 400
            },
            waiting: {
                autoShow: false,
            }
        });
    },1000);
}

var wave = (function () {
	var colorInc = 100 / 3;
	var val = $('.percent').find('span').text();
	
	if (val != "" && !isNaN(val) && val <= 100 && val >= 0) {
	    console.log(val);
	
	    var valOrig = val;
	    val = 100 - val;
	
	    if (valOrig == 0) {
	        $(".progress .percent").text(0 + "%");
	    } else{
	    	$(".progress .percent").text(valOrig + "%");
	    } 
	
	    $(".progress").parent().removeClass();
	    $(".progress .water").css("top", val + "%");
	
	    if (valOrig < colorInc * 1){
	        $(".progress").parent().addClass("red");
	    }
	    else if (valOrig < colorInc * 2){
	        $(".progress").parent().addClass("orange");
	    }
	    else{
	        $(".progress").parent().addClass("green");
	    }
	}
});
